package com.start.boot.web;


import com.start.boot.dao.ajpc.QueryMapper;
import com.start.boot.domain.Tongji;
import com.start.boot.pojo.dto.CountDo;
import com.start.boot.pojo.vo.CountVo;
import com.start.boot.service.CountService;
import com.start.boot.support.utils.EasyUIHelper;
import com.start.boot.support.utils.FastJsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Api("统计接口")
@Controller
@RequestMapping("/count")
public class CountController {

    @Autowired
    CountService countService;

    @Autowired
    QueryMapper queryMapper;

    @ApiOperation("案件质量年度趋势图")
    @ResponseBody
    @RequestMapping("/getCount")
    public Object getCount(Integer year,String pcflbm){
        CountDo countDo=new CountDo();
        List<String> pljgType = queryMapper.getPljgType();
        List<CountDo> init = countDo.init(pljgType);
        try {
            Map<Integer, List<CountDo>> pcByYear = countService.getPcByYear(year, pcflbm);
            Set<CountVo> voArrayList = new HashSet<>();
            if (pcByYear!=null){
                Set<Map.Entry<Integer, List<CountDo>>> entries = pcByYear.entrySet();
                entries.stream().forEach(data->{
                    for (CountDo aDo : init) {
                        if (!data.getValue().contains(aDo)){
                            data.getValue().add(aDo);
                        }
                    }
                });
                entries.stream().forEach(data-> {
                    CountVo vo = new CountVo();
                    vo.setName(data.getKey());
                    vo.setValue(data.getValue());
                    voArrayList.add(vo);
                });
            }
            List<CountVo> collect = voArrayList.stream().sorted(Comparator.comparing(CountVo::getName)).collect(Collectors.toList());
            return collect;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    @ApiOperation("案件质量年度饼图")
    @ResponseBody
    @RequestMapping("/getYearCount")
    public Object getYearCount(Integer year,String pcflbm){
        try {
            return countService.getYearPercentByYearAndPcflbm(year,pcflbm);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @ApiOperation("问题性质分析统计分析数据表格")
    @RequestMapping("/getPcxFlByYearAndMbbmAndPcjg")
    @ResponseBody
    public Object getPcxFlByYearAndMbbmAndPcjg(String pcflbm,Integer year){
        try {
            return countService.getPcxFlByYearAndMbbmAndPcjg(pcflbm, year);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    @ApiOperation("问题性质分析统计分析饼图")
    @RequestMapping("/getPcxFlByYearAndBinTu")
    @ResponseBody
    public Object getPcxFlByYearAndBinTu(String pcflbm,Integer year,String pcjg){
        try {
            return countService.getPcxFlByYearAndBinTu( pcflbm, year,pcjg);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/getAjzlLinesByYearAndPcflbm")
    @ResponseBody
    public Object getAjzlLinesByYearAndPcflbm(Integer year,String pcflbm){
        try {
            return countService.getAjzlLinesByYearAndPcflbm(year, pcflbm);
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }

    @RequestMapping("/getAjzlPieByYearAndPcflbm")
    @ResponseBody
    public Object getAjzlPieByYearAndPcflbm(Integer year,String pcflbm){
        try {
            return countService.getAjzlPieByYearAndPcflbm(year, pcflbm);
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }


    @ApiOperation("基本情况统计分析总表数据")
    @RequestMapping("/getJbpcTotal")
    @ResponseBody
    public Object getJbpctotal(String json) {

        try{
            Tongji tongji = FastJsonUtils.toObject(Tongji.class, json);

            List<Map> list = countService.getJbpctotal(tongji);

            return EasyUIHelper.buildDataGridDataSource(list,list.size());
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 待完成
     * @param json
     * @return
     */
    @ApiOperation("基本情况统计分析案件类别")
    @RequestMapping("/getJbpcCategory")
    @ResponseBody
    public Object getJbpcCategory(String json) {
        Tongji tongji = FastJsonUtils.toObject(Tongji.class, json);

        return null;

    }

    @ApiOperation("基本情况统计分析问题性质")
    @RequestMapping("/getJbpcProblem")
    @ResponseBody
    public Object getJbpcProblem(String json) {
        Tongji tongji = FastJsonUtils.toObject(Tongji.class, json);

        return null;
    }


    @ApiOperation("基本情况统计分析评查结论")
    @RequestMapping("/getJbpcConclusion")
    @ResponseBody
    public Object getJbpcConclusion(String json) {
        Tongji tongji = FastJsonUtils.toObject(Tongji.class, json);

        return null;
    }



}
